package com.yykt.weshare.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.yykt.weshare.util.ExecutorUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;

/**
 * app 级别通用 bean 配置
 */
@Configuration
public class AppConfig {
    public static final Integer THREAD_NUM = 2;
    public static final String APP_BASE_THREAD_POOL = "APP_BASE_THREAD_POOL";

    // autowire 默认按照类型注入, 可以指定 @Primary指定主要的 bean 或者指定 @Qualifier
    @Bean(name = "objectMapper")
    public ObjectMapper getObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        // 使得序列化的json字符串中不包含null值
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        return mapper;
    }

    @Bean(name = APP_BASE_THREAD_POOL)
    public Executor universalThreadPool() { // 注意，这里不要用 common !!!容易遇到坑
        return ExecutorUtils.getExecutor("app-", THREAD_NUM);
    }
}
